# coding=utf-8
"""
    @project: wlou
    
    @file： csv_parse_qa_handle.py
    @date：2024/5/21 14:59
    @desc:
"""
import json
import io

from charset_normalizer import detect

from common.handle.base_parse_qa_handle import BaseParseQAHandle, get_title_row_index_dict, get_row_value


class JsonParseQAHandle(BaseParseQAHandle):
    def support(self, file, get_buffer):
        file_name: str = file.name.lower()
        if file_name.endswith(".jsonl"):
            return True
        return False

    def handle(self, file, get_buffer, save_image):
        buffer = get_buffer(file)
        try:
            readers = io.TextIOWrapper(io.BytesIO(buffer), encoding=detect(buffer)['encoding']).readlines()
            paragraph_list = []
            for reader in readers:
                row = json.loads(reader)
                conversation = row['conversation']
                if conversation is None or len(conversation) == 0:
                    continue
                problem = conversation[0]['human']
                problem = str(problem) if problem is not None else ''
                problem_list = [{'content': p[0:255]} for p in problem.split('\n') if len(p.strip()) > 0]
                title = row['system']
                title = str(title) if title is not None else ''
                paragraph_list.append({'title': title,
                                       'content': conversation[0]['assistant'],
                                       'problem_list': problem_list})
                print("上传后内容", paragraph_list)
            return [{'name': file.name, 'paragraphs': paragraph_list}]
        except Exception as e:
            return [{'name': file.name, 'paragraphs': []}]
